<?php
	class PdoUtil {
		private $db;
		private static $instance;
		
		private function __construct() {
			try {
				$this->db = new PDO('mysql:host=localhost;dbname=mydb;charset=UTF-8', 'root', '');
			} catch (Exception $e) {
				die('Erreur : ' . $e->getMessage());
			}
		}
		
		public static function getInstance() {
			if (is_null(self::$instance)) {
				self::$instance = new PdoUtil();
			}
			
			return self::$instance;
		}
		
		public function executeSelect($query, $params) {
			$results = array();
			
			$req = $this->db->prepare($query);
			$req->execute($params);
			
			while ($donnees = $req->fetch()) {
				array_push($results, $donnees);
			}
			
			$req->closeCursor();
			
			return $results;
		}		
		
		public function execute($query, $params) {
			$req = $this->db->prepare($query);
			$req->execute($params);
		}
		
		public function lastinsertedId() {
			return $this->db->lastInsertId();
		}
	}
?>